package mud

import grails.plugin.jms.Queue
import grails.plugin.jms.Subscriber

import javax.jms.Connection
import javax.jms.Message
import javax.jms.MessageProducer;
import javax.jms.Session

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator
import javax.jms.Topic
import javax.jms.TextMessage;

class UserActionService {

	static exposes = ['jms']
	
	def jmsService;
	Topic chatTopic;
	JmsTemplate defaultJmsTemplate;
		
	@Subscriber( topic = "chat" )
	def chat(msg){
		println( "->$msg" );
	}
	
	@Queue( name = "do", adapter = "passthru" )
	def doAction(msg){
		println( msg.userID );
        MessageCreator msgCreator = { Session session ->
            TextMessage textMessage = session.createTextMessage("action: $msg.text")
            return textMessage
        } as MessageCreator
		defaultJmsTemplate.send( chatTopic, msgCreator )
	}
}
